package com.atgugiu.seckill.schedule;

import com.atgugiu.seckill.service.SecKillService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class SecKillScheduled {

  @Resource
  private RedissonClient redissonClient;

  @Resource
  private SecKillService secKillService;

  //秒杀商品上架功能的锁
  private final String upload_lock = "seckill:upload:lock";

  /**
   * 定时任务
   * 每天三点上架最近三天的秒杀商品
   */
  @Async
  @Scheduled(cron = "0 0 3 * * ?")
//    @Scheduled(cron = "* 33 17 * * ?")
  public void uploadSeckillSkuLatest3Days() {
    //为避免分布式情况下多服务同时上架的情况，使用分布式锁
    RLock lock = redissonClient.getLock(upload_lock);
    try {
      lock.lock(10, TimeUnit.SECONDS);
      secKillService.uploadSeckillSkuLatest3Days();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      lock.unlock();
    }
  }
}
